package game.menu;

import game.Input.MenuAction;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

public class Button implements Drawable {
	public boolean highlighted = false;
	private MenuAction menuAction;
	private Rectangle2D.Double r;
	private String buttonTitle;
	private Menu m;

	public Button(double x, double y, double w, double h, String s,
			MenuAction menuAction, Menu m) {
		r = new Rectangle2D.Double(x, y, w, h);
		this.menuAction = menuAction;
		buttonTitle = s;
		this.m = m;
	}

	public MenuAction getMenuAction() {
		return menuAction;
	}

	public void draw(Graphics g) {
		// Draws the rectangle for the button
		if (!highlighted)
			g.setColor(Color.gray);
		else
			g.setColor(Color.white);
		g.fillRect((int) (r.x * m.getWidth()), (int) (r.y * m.getHeight()),
				(int) (r.width * m.getWidth()),
				(int) (r.height * m.getHeight()));

		if (!highlighted)
			g.setColor(Color.white);
		else
			g.setColor(Color.black);

		// Sets the font for the text
		Font font = new Font("Arial", Font.PLAIN, 50);
		g.setFont(font);
		printTextCenter(font, new Rectangle((int) (r.x * m.getWidth()),
				(int) (r.y * m.getHeight()), (int) (r.width * m.getWidth()),
				(int) (r.height * m.getHeight())), g);
	}

	/**
	 * Centers the text from a given rectangle
	 * 
	 * @param s The string you want to paint onto the screen
	 * @param f The current font you are using
	 * @param rectangle The rectangle you want to center the text on
	 * @param g The graphics g
	 */
	private void printTextCenter(Font font, Rectangle rectangle, Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		FontRenderContext frc = g2.getFontRenderContext();
		float sw = (float) font.getStringBounds(buttonTitle, frc).getWidth();
		LineMetrics lm = font.getLineMetrics(buttonTitle, frc);
		float sh = lm.getAscent() + lm.getDescent();
		
		// center text in r
		float sx = (float) (rectangle.x + (rectangle.width - sw) / 2);
		float sy = (float) (rectangle.y + (rectangle.height + sh) / 2 - lm.getDescent());
		g2.drawString(buttonTitle, sx, sy);
	}

	/**
	 * Returns true if the the rectangle (button) contains that point
	 * 
	 * @param p The point we are looking at
	 * @return True if r contains p, false otherwise
	 */
	public boolean containsPoint(Point p) {
		return new Rectangle((int) (r.x * m.getWidth()),
				(int) (r.y * m.getHeight()), (int) (r.width * m.getWidth()),
				(int) (r.height * m.getHeight())).contains(p);
	}

	public String getButtonTitle() {
		return buttonTitle;
	}

	public boolean isHighlighted() {
		return highlighted;
	}

	public void setHighlighted(boolean highlighted) {
		this.highlighted = highlighted;
	}
}
